<?php
include('includes/connexion.php');
$adminmanager = new AdminManager($connexion);
$elevemanager = new EleveManager($connexion);
session_start();
include('includes/secu.php');

$exercicemanager = new ExerciceManager($connexion);// on instancie un exercice avec la connexion en parametre
$testmanager = new TestManager($connexion); //  on instancie un test pour récupérer le nom du test
$testexercicemanager = new Test_ExerciceManager($connexion);
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>Tables Advanced - Target Admin</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="./css/font-awesome.min.css">
  <link rel="stylesheet" href="./js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="./css/bootstrap.min.css">

  <!-- Plugin CSS -->
  <link rel="stylesheet" href="./js/plugins/icheck/skins/minimal/blue.css">
  <link rel="stylesheet" href="js/plugins/select2/select2.css">
  <link rel="stylesheet" href="js/plugins/datepicker/datepicker.css">
  <link rel="stylesheet" href="js/plugins/simplecolorpicker/jquery.simplecolorpicker.css">
  <link rel="stylesheet" href="js/plugins/timepicker/bootstrap-timepicker.css">
  <link rel="stylesheet" href="js/plugins/fileupload/bootstrap-fileupload.css">
  
  <!-- App CSS -->
  <link rel="stylesheet" href="./css/target-admin.css">
  <link rel="stylesheet" href="./css/custom.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
</head>
<?php 
	if(!empty($_GET['idTest'])){
		$idTest = $_GET['idTest'];
		$tabExercice = $exercicemanager->getByTest($idTest); // on récupere la liste des exercices dans le tableau
		$listExercice = $exercicemanager->getList();
	}
		
	if(!empty($_POST))
	{
		foreach($_POST as $key => $donnees)
		{
			if($key == "idTest")
			{
				$idTest = $donnees;
			} else {
				$exercices[] = $donnees;
			}
		}
		
		$testmanager->deleteAllExercices($idTest);
		
		foreach($exercices as $ex)
		{
			$test_ex = new Test_Exercice();
			$test_ex->setIdRefTest($idTest);
			$test_ex->setIdRefExercice($ex);
			$testexercicemanager->add($test_ex);
		}
		
		header('Location: exercice.php?idTest='.$idTest);
	}
	
	//Si on change l'ordre des exercices
	if(!empty($_GET['ordre']) && !empty($_GET['id'])){
		$test_exo = $testexercicemanager->get($_GET['idTest'], $_GET['id']);
		
		if ($_GET['ordre']=="up"){
			$testexercicemanager->ordreUp($test_exo->idRefTest(), $test_exo->idRefExercice());
		} else if($_GET['ordre'] == "down"){
			$testexercicemanager->ordreDown($test_exo->idRefTest(), $test_exo->idRefExercice());
		}
		header('Location:exercice.php?idTest='.$idTest); // on redirige	
	}			
?>
<body>

<?php include('includes/navbar.php'); ?>

<?php include('includes/mainbar.php'); ?>

<div class="container">

  <div class="content">

    <div class="content-container">

      

      <div class="content-header">
        <h2 class="content-header-title">Exercices</h2>
        <a href="gestion.php">Tests</a> / Exercices
      </div> <!-- /.content-header --> 

      <div class="row">
      

        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Liste des exercices du test <?php echo $testmanager->get($idTest)->intitule(); ?>
              </h3>

            </div> <!-- /.portlet-header -->

            <div class="portlet-content">           
			<form action="exercice.php" method="post">
            <input type="text" value="<?php echo $idTest; ?>" name="idTest" hidden/>
<?php		foreach($testexercicemanager->getByTest($idTest) as $test_exercice){
				$exercice = $exercicemanager->get($test_exercice->idRefExercice());
				echo "<div class='checkbox'><label> | <a href ='exercice.php?id=".$exercice->id()."&ordre=up&idTest=".$idTest."' style='color:blue;'><span class='fa fa-angle-double-up'></span></a> | ".$test_exercice->ordre()." | <a href ='exercice.php?id=".$exercice->id()."&ordre=down&idTest=".$idTest."' style='color:blue;'><span class='fa fa-angle-double-down'></span></a> | ";
				echo "<input type='checkbox' value='".$exercice->id()."' name='exercice".$exercice->id()."' checked> ".$exercice->intitule()." (".$exercice->duree_seconde()." secondes)<label></div>";
			}
			foreach($listExercice as $exercice)
			{
				if(!$testmanager->contientExercice($idTest, $exercice->id()))
				{
					echo "<div class='checkbox'><label><input type='checkbox' value='".$exercice->id()."' name='exercice".$exercice->id()."'> ".$exercice->intitule()." (".$exercice->duree_seconde()." secondes)<label></div>";
				}
			}
		?>
        <button type="submit" class="btn btn-primary">Enregistrer</button>
        </form>
            </div> <!-- /.portlet-content -->

          </div> <!-- /.portlet -->

        </div> <!-- /.col -->

      </div> <!-- /.row -->

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->


<?php include('includes/footer.php'); ?>

  <script src="./js/libs/jquery-1.10.1.min.js"></script>
  <script src="./js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="./js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->
  
  <!-- Plugin JS -->
  <script src="./js/plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="./js/plugins/datatables/DT_bootstrap.js"></script>
  <script src="./js/plugins/tableCheckable/jquery.tableCheckable.js"></script>
  <script src="./js/plugins/icheck/jquery.icheck.min.js"></script>

  <!-- App JS -->
  <script src="./js/target-admin.js"></script>
  
</body>
</html>